package com.example.ssmp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ssmp.bean.Book;
import com.example.ssmp.dao.IBookDao;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<IBookDao, Book> implements IBookService{

    @Autowired
    IBookDao bookDao;

    @Override
    public boolean saveBook(Book book) {
        return bookDao.insert(book)>0;
    }

    @Override
    public boolean modify(Book book) {
        return bookDao.updateById(book)>0;
    }

    @Override
    public boolean delete(Integer id) {
        return bookDao.deleteById(id) >0;
    }

    @Override
    public List<Book> getby(String value) {
        LambdaQueryWrapper<Book> lw=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Book> like = lw.like(value != null, Book::getName, value);
        List<Book> books = bookDao.selectList(like);
        return books;
    }

    public  Page<Book> getPage(int current, int size) {
        Page<Book> page=new Page<>(current,size);
        Page<Book> bookPage = bookDao.selectPage(page, null);
        return bookPage;
    }

    public  Page<Book> getPage(int current, int size,Book book) {
        Page<Book> page=new Page<>(current,size);
        LambdaQueryWrapper<Book> lq=new LambdaQueryWrapper<>();
        lq.like(Strings.isNotEmpty(book.getName() ),Book::getName,book.getName());
        lq.like(Strings.isNotEmpty(book.getType() ),Book::getType,book.getType());
        lq.like(Strings.isNotEmpty(book.getDescription() ),Book::getDescription,book.getDescription());
        Page<Book> bookPage = bookDao.selectPage(page, lq);
        return bookPage;
    }
}
